<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools :show-before="false">
        <template v-slot:after>
          <el-button type="primary" size="small" @click="onClickAdd(1,'0')">添加权限</el-button>
        </template>
      </PageTools>
      <el-table ref="table" :data="tanleDate" row-key="id">
        <el-table-column label="名称">
          <template v-slot="{ row }">
            <i
              v-if="row.type === 1"
              style="margin-right: 5px;"
              class="el-icon-folder-opened"
              @click="expend(row)"
            />
            <i
              v-else
              style="margin-right: 5px;"
              class="el-icon-view"
            />
            <span class="right-name">{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" type="text" @click="onClickAdd(2,row.id)">添加</el-button>
            <el-button type="text" @click="onEdit(row.id)">编辑</el-button>
            <el-button type="text" @click="onDel(row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>

      <!--  -->
      <el-dialog
        :visible.sync="updatePermissionDialog"
        width="30%"
        @closed="onClose"
      >
        <template v-slot:title>
          {{ title ? '编辑权限' : '添加权限' }}
        </template>
        <el-form ref="form" :model="addOrEditForm" :rules="rules" label-width="80px">
          <el-form-item label="活动名称" prop="name">
            <el-input v-model="addOrEditForm.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="addOrEditForm.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="addOrEditForm.description" />
          </el-form-item>
          <el-form-item label="权限开启">
            <el-switch
              v-model="addOrEditForm.enVisible"
              active-color="#13ce66"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="onClose">取 消</el-button>
          <el-button type="primary" @click="onSave">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, updatePermissionList, getPermissionInfo, updatePermissionInfo, delPermission } from '@/api/permission.js'
import { transListToTree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      tanleDate: [],
      updatePermissionDialog: false,
      addOrEditForm: {
        description: '',
        code: '',
        name: '',
        enVisible: ''
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      },
      title: false
    }
  },

  created() {
    this.loadPermissionList()
  },

  methods: {
    // 获取全部权限列表
    async loadPermissionList() {
      const res = await getPermissionList()
      this.tanleDate = transListToTree(res, '0')
    },

    // 点击图标触发
    expend(row) {
      row.isExpend = !row.isExpend
      this.$refs.table.toggleRowExpansion(row, row.isExpend)
    },

    // 点击取消
    onClose() {
      this.updatePermissionDialog = false
      setTimeout(() => {
        this.addOrEditForm = {
          description: '',
          code: '',
          name: '',
          enVisible: ''
        }
        this.title = false
      }, 200)
    },

    // 点击确定
    async onSave() {
      try {
        await this.$refs.form.validate()
        if (this.addOrEditForm.id) {
          // 编辑
          await updatePermissionInfo(this.addOrEditForm)
          this.$message.success('更新成功')
        } else {
          await updatePermissionList(this.addOrEditForm)
          this.loadPermissionList()
          this.$message.success('添加成功')
        }
        this.onClose()
      } catch (error) {
        this.$message.error('添加失败')
      }
    },

    // 点击添加
    onClickAdd(type, pid) {
      this.addOrEditForm.type = type
      this.addOrEditForm.pid = pid
      this.updatePermissionDialog = true
    },

    // 点击编辑
    async onEdit(id) {
      this.title = true
      this.updatePermissionDialog = true
      this.addOrEditForm = await getPermissionInfo(id)
    },

    // 点击删除
    async onDel(id) {
      await delPermission(id)
      this.$message.success('删除成功')
      this.loadPermissionList()
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-table__expand-icon {
  display: none!important
}
</style>
